package com.haiyou.dc.thread.demo1;

public class TestVisibility {

    private volatile Integer a = 0;

    private synchronized void increase() {
        a++;
    }

    public static void main(String[] args) {

        TestVisibility visibility = new TestVisibility();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    visibility.increase();
                }
            }).start();
        }
        System.out.println(Thread.activeCount()); // 结果为2
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }

        System.out.println(visibility.a);
    }

}
